﻿namespace LinesOfAction.Extensions
{
    using Enumerations;

    public static class Point
    {
         public static bool IsValid(this Model.Point p)
         {
             var x = p.X;
             var y = p.Y;

             return (x <= 7) && (y <= 7);
         }

        public static PieceColors Color(this Model.Point p, Model.Checkerboard c)
        {
            var test = PieceColors.Invalid;

            if (p.IsValid())
            {
                if (p.IsWhite(c))
                {
                    test = PieceColors.White;
                }
                else
                {
                    if (p.IsBlack(c))
                    {
                        test = PieceColors.Black;
                    }
                }
            }

            return test;
        }
        
        private static int Offset(this Model.Point p)
        {

            return p.Y * 8 + p.X;
        }

        private static bool IsWhite(this Model.Point p, Model.Checkerboard c) 
        {
            var isWhite = false;
            
            var mask = c.WhitePieces >> p.Offset();

            mask &= 1;
            if (mask == 1) {
                isWhite = true;
            }

            return isWhite;
        }

        private static bool IsBlack(this Model.Point p, Model.Checkerboard c) 
        {
            var isBlack = false;

            var mask = c.BlackPieces >> p.Offset();

            mask &= 1;
            if (mask == 1) {
                isBlack = true;
            }

            return isBlack;
        }

        private static bool IsEmpty(this Model.Point p, Model.Checkerboard c)
        {
            return !(p.IsWhite(c) || p.IsBlack(c));
        }
    }
}